package com.jiangxjsy.gateway.configuration;

import com.jiangxjsy.core.exception.FeignServiceException;
import com.jiangxjsy.core.wrap.Result;
import com.jiangxjsy.core.wrap.ResultHelper;
import feign.FeignException;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.Response;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/1 10:53
 *
 * <p>
 * 自定义 Open-Feign 配置类
 * </p>
 *
 * <p>
 * 注意：
 * 为了兼容微服务项目中已有组件，此配置类必须存在，
 * 即为了使 client.AuthorizationClient 能正常与 authorization 服务进行通信，
 * 确保鉴权过滤器 filter.AuthorizationFilter 中的鉴权功能正常，
 * 必须存在该配置类！
 * </p>
 */

@Configuration
public class FeignConfiguration implements RequestInterceptor {
    /**
     *  自定义 feign 解码器
     *
     * @return  Decoder
     */
    @Bean
    public Decoder feignDecoder() {
        return new ResponseEntityDecoder(new FeignDecode(feignHttpMessageConverter()));
    }

    /**
     * 自定义 feign 消息转换器
     *
     * @return ObjectFactory
     */
    private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
        final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(
                new GateWayMappingJackson2HttpMessageConverter()
        );

        return () -> httpMessageConverters;
    }

    @Override
    public void apply(RequestTemplate requestTemplate) {

    }

    /**
     *  自定义 feign 消息转换器
     */
    public static class GateWayMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        GateWayMappingJackson2HttpMessageConverter() {
            List<MediaType> mediaTypes = new ArrayList<>();
            mediaTypes.add(MediaType.APPLICATION_JSON);
            setSupportedMediaTypes(mediaTypes);
        }
    }

    /**
     *  自定义 feign 解码器
     */
    public static class FeignDecode extends SpringDecoder {
        FeignDecode(ObjectFactory<HttpMessageConverters> messageConverters) {
            super(messageConverters);
        }

        @Override
        public Object decode(Response response, Type type) throws IOException, FeignException {
            Object data = super.decode(response, type);
            if (data instanceof Result) {
                if (!ResultHelper.hasSuccess((Result<?>) data)) {
                    throw new FeignServiceException(((Result<?>) data).getCode(), ((Result<?>) data).getMsg());
                }
            }
            return data;
        }
    }
}
